﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using SalePartial.Utility;
using XunPan.Web.Globals;
using SalePartial.Model;
using SalePartial.BLL;
using System.Data;
using Telerik.Web.UI;
using System.IO;



public partial class Trade_Inbox_MailBoxList2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
             
        {
            LoginUserType type = RoleControlManager.checkUserAuthority(User.Identity.Name);
            if (type == LoginUserType.FREE_SUPPLY
                || type == LoginUserType.CERT_PURCHASE || type == LoginUserType.CERT_SUP_PUR
                || type == LoginUserType.FREE_SUP_PUR)
            {
            if (User.Identity.IsAuthenticated)
            {
                MembershipUser member = Membership.GetUser(User.Identity.Name);
                //MembershipUser member = Membership.GetUser("jianglanshui@gmail.com");
                if (member != null)
                {
                    ViewList();
                    sdsMailList.SelectParameters["CreateUserID"].DefaultValue = member.ProviderUserKey.ToString();

                    sdsMailList.FilterParameters["Filter1"].DefaultValue = " RecipientUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "' and   SUBSTRING([Status], 5, 1)<>'" + 2 + "'";
                }
                else
                {
                    Response.Redirect(GlobalSetting.UrlHomePage, true);
                }

                if (Roles.IsUserInRole(User.Identity.Name, "Purchase"))
                {
                    sdsCompanyList.FilterParameters["UserRole"].DefaultValue = "VendorModerateStatus";

                }
                else if (Roles.IsUserInRole(User.Identity.Name, "Suppliers"))
                {
                    sdsCompanyList.FilterParameters["UserRole"].DefaultValue = "BuyerModerateStatus";
                }
                else
                {
                    Response.Redirect(GlobalSetting.UrlHomePage, true);
                }

                string companyid = Request.QueryString.Get("v");

                if (GlobalUtility.IsValidGuid(companyid))
                {
                    try
                    {
                        XPCompany xpcomp = new XPCompany();
                        XPCompanyInfo company = xpcomp.GetXPCompany(new Guid(companyid));
                        NewMessage();

                        rcbRecipient.Text = company.CompanyName;
                        rcbRecipient.SelectedValue = companyid;


                    }
                    catch (Exception ex)
                    {
                        //no such company info
                    }
                }
                //我的邮件
                string displaytext = rpbNavigator.FindItemByValue("All").Text;

                for (int i = 0; i <= 8; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("All").Text = displaytext;

                displaytext = rpbNavigator.FindItemByValue("Normal").Text;
                for (int i = 0; i <= 8; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("Normal").Text = displaytext;

                //采购商反馈信息
                displaytext = rpbNavigator.FindItemByValue("Business").Text;

                for (int i = 0; i <= 5; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("Business").Text = displaytext;

                //草稿
                displaytext = rpbNavigator.FindItemByValue("Saved").Text;

                for (int i = 0; i <= 10; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("Saved").Text = displaytext;

                //已发送邮件
                displaytext = rpbNavigator.FindItemByValue("Sent").Text;

                for (int i = 0; i <= 7; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("Sent").Text = displaytext;


                //重要邮件
                displaytext = rpbNavigator.FindItemByValue("Important").Text;

                for (int i = 0; i <= 8; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("Important").Text = displaytext;

                //垃圾邮件
                displaytext = rpbNavigator.FindItemByValue("Rubbish").Text;

                for (int i = 0; i <= 8; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("Rubbish").Text = displaytext;


                InitializeEventRecordsOnMenuPanel();
            }
            }
            else
            {
                Response.Redirect(GlobalSetting.UrlHomePage);

            }

        }
        string p = rcbRecipient.SelectedValue;

        Trace.Warn(p);

           
    }

    private void InitializeEventRecordsOnMenuPanel()
    {
        //    '初始化所有数据记录数
        string cmdtxt = sdsMailList.SelectCommand;

        //sdsMailList.FilterParameters["Filter1"].DefaultValue = "  (RecipientUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "' OR CreateUserID='" 
        //        + sdsMailList.SelectParameters["CreateUserID"].DefaultValue+"')";

        DataView dv = (DataView)sdsMailList.Select(DataSourceSelectArguments.Empty);

        string displaytext = rpbNavigator.FindItemByValue("Normal").Text;

        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);

        dv.RowFilter = "  RecipientUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "' AND SUBSTRING([Status], 2, 1) = '1'  AND SUBSTRING([Status], 4, 1) = '0'  AND SUBSTRING([Status], 5, 1) <> '2'";
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("Normal").Text = displaytext;

        displaytext = rpbNavigator.FindItemByValue("All").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        dv.RowFilter = "  RecipientUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "' AND SUBSTRING([Status], 2, 1) = '1' AND SUBSTRING([Status], 5, 1) <> '2'";
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("All").Text = displaytext;

        displaytext = rpbNavigator.FindItemByValue("Business").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        dv.RowFilter = "  RecipientUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "' AND SUBSTRING([Status], 2, 1) = '1'  AND SUBSTRING([Status], 4, 1) = '1'  AND SUBSTRING([Status], 5, 1) <>'2'";
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("Business").Text = displaytext;

        displaytext = rpbNavigator.FindItemByValue("Saved").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        dv.RowFilter = " CreateUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "'  AND SUBSTRING([Status], 1, 1) = '1'  AND SUBSTRING([Status], 3, 1) <> '2'";
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("Saved").Text = displaytext;

        displaytext = rpbNavigator.FindItemByValue("Sent").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        dv.RowFilter = " CreateUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "'  AND SUBSTRING([Status], 2, 1) = '1' AND SUBSTRING([Status], 3, 1) <> '2'";
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("Sent").Text = displaytext;

        displaytext = rpbNavigator.FindItemByValue("Important").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        dv.RowFilter = " ((CreateUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "'  AND SUBSTRING([Status], 3, 1) = '1') " +
                 " OR (RecipientUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "'  AND SUBSTRING([Status], 5, 1) = '1')) ";
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("Important").Text = displaytext;

        displaytext = rpbNavigator.FindItemByValue("Rubbish").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        dv.RowFilter = " ((CreateUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "'  AND SUBSTRING([Status], 3, 1) = '2') " +
                 " OR (RecipientUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "'  AND SUBSTRING([Status], 5, 1) = '2' ))";
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("Rubbish").Text = displaytext;

    }

    protected void btnSend_Click(object sender, System.EventArgs e)
    {


        XPNotificationInfo msg = new XPNotificationInfo();
        XPNotification xpNotifi = new XPNotification();

        if (Updflg.Text == "NewMsg")
        {
            msg.CreateUserID = new Guid(sdsMailList.SelectParameters["CreateUserID"].DefaultValue);
            msg.Status = "0100000";
            msg.CreateDate = DateTime.Now;

        }
        else
        {
            Guid NotifyID = new Guid(sdsViewDetail.SelectParameters["NotifyID"].DefaultValue);
            msg = xpNotifi.GetXPNotification(NotifyID);
            msg.Status = msg.Status.Remove(1, 1).Insert(1, "1");
        }
        if (rcbRecipient.SelectedValue == "")
        {
            lblMsg.Text = "请选择收件人";
            return;
        }
        if (tbTitle.Text  == "")
        {
            lblMsg1.Text = "请输入主题";
            return;
        }
       
    
        

            msg.RecipientUserID = new Guid(rcbRecipient.SelectedValue);
            msg.Attachment = lnkAttachment.NavigateUrl;
            
            
            msg.BodyText = radEditorContent.Content;
            msg.SendDate = DateTime.Now;
           
            msg.Title = tbTitle.Text;


            msg.ContentType = 0;
            if (Updflg.Text == "NewMsg")
            {
                xpNotifi.Insert(msg);
            }
            else
            {
                xpNotifi.Update(msg);
            }

            rcbRecipient.ClearSelection();
            rcbRecipient.Text = "";
            radEditorContent.Content = "";
            tbTitle.Text = "";
           
            lnkAttachment.NavigateUrl = "";
            lnkAttachment.Text = "";
       // lnkAttachment.NavigateUrl=

            Response.Redirect("MailBoxList2.aspx");
            
       

            //DataBind();

            InitializeEventRecordsOnMenuPanel();
            
        }
    
    


    protected void radGridMainlist_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {

        try
        {
            switch (e.CommandName)
            {
                case "viewdetail":
                case "viewdetails":
                    //ViewDetail();

                    sdsViewDetail.SelectParameters["NotifyID"].DefaultValue = ((GridDataItem)e.Item).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["NotifyID"].ToString();
                    //rptMessageDetail.DataBind();
                    string radalertscript = "<script language='javascript'>function f(){radopen(\"MailBoxDetails.aspx?NotifyID=" + sdsViewDetail.SelectParameters["NotifyID"].DefaultValue + "\", null); Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>";

                    if ((rpbNavigator.SelectedItem.Value == "Normal"|| rpbNavigator.SelectedItem.Value == "Business" )
                        && e.CommandName == "viewdetails" && e.CommandArgument.ToString().Substring(3, 1) == "0")
                    {
                        try
                        {
                            Guid NotifyID = new Guid(sdsViewDetail.SelectParameters["NotifyID"].DefaultValue);
                            XPNotification xpNotifi = new XPNotification();
                            XPNotificationInfo msg = xpNotifi.GetXPNotification(NotifyID);
                            msg.Status = msg.Status.Remove(3, 1).Insert(3, "1");
                            xpNotifi.Update(msg);

                            rpbNavigator_ItemClick(null, new RadPanelBarEventArgs(rpbNavigator.SelectedItem));
                            InitializeEventRecordsOnMenuPanel();
                        }
                        catch (Exception)
                        {
                        }
                    }
                    else if (e.CommandName == "viewdetails" && rpbNavigator.SelectedItem.Value == "Saved")
                    {
                        Guid NotifyID = new Guid(sdsViewDetail.SelectParameters["NotifyID"].DefaultValue);

                        XPNotification xpNotifi = new XPNotification();
                        XPNotificationInfo msg = xpNotifi.GetXPNotification(NotifyID);

                        EditMessage(msg);
                        break;
                    }

                    RadScriptManager.RegisterStartupScript(this, this.GetType(), "radalert", radalertscript, false);
                    break;
                case "InitInsert":
                    NewMessage();
                    //e.Canceled = true;
                    break;
                case "RebindGrid":
                    rpbNavigator_ItemClick(null, new RadPanelBarEventArgs(rpbNavigator.SelectedItem));
                    InitializeEventRecordsOnMenuPanel();
                    break;
                case "ChangeImportance":

                    sdsViewDetail.SelectParameters["NotifyID"].DefaultValue = ((GridDataItem)e.Item).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["NotifyID"].ToString();
                    try
                    {

                        Guid NotifyID = new Guid(sdsViewDetail.SelectParameters["NotifyID"].DefaultValue);

                        XPNotification xpNotifi = new XPNotification();
                        XPNotificationInfo msg = xpNotifi.GetXPNotification(NotifyID);
                        int index = 0;
                        //接收者

                        if (rpbNavigator.SelectedItem.Value == "Normal" || rpbNavigator.SelectedItem.Value == "All"||
                                rpbNavigator.SelectedItem.Value == "Business")
                            {
                                index = 4;
                            }
                            else
                            {
                                index = 2;
                            }
                            if (msg.Status.Substring(index, 1) == "0")
                            {
                                msg.Status = msg.Status.Remove(index, 1).Insert(index, "1");
                            }
                            else if (msg.Status.Substring(index, 1) == "1")
                            {
                                msg.Status = msg.Status.Remove(index, 1).Insert(index, "0");
                            }
                            xpNotifi.Update(msg);
                       

                        rpbNavigator_ItemClick(null, new RadPanelBarEventArgs(rpbNavigator.SelectedItem));
                        InitializeEventRecordsOnMenuPanel();
                        //ViewList();

                        //sdsMailList.FilterExpression = " ((CreateUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "'  AND SUBSTRING([Status], 3, 1) = '1') " +
                        //     " OR (RecipientUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "'  AND SUBSTRING([Status], 5, 1) = '1')) ";
                        //radGridMainlist.Rebind();
                    }
                    catch (Exception ex)
                    {
                        return;

                    }
                    break;
                case "Delete":

                    sdsViewDetail.SelectParameters["NotifyID"].DefaultValue = ((GridDataItem)e.Item).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["NotifyID"].ToString();
                    try
                    {

                        Guid NotifyID = new Guid(sdsViewDetail.SelectParameters["NotifyID"].DefaultValue);

                        XPNotification xpNotifi = new XPNotification();
                        XPNotificationInfo msg = xpNotifi.GetXPNotification(NotifyID);
                        int index = 0;
                        //接收者

                        if (rpbNavigator.SelectedItem.Value != "Rubbish")
                        {
                            if (rpbNavigator.SelectedItem.Value == "Normal" || rpbNavigator.SelectedItem.Value == "All" ||
                                rpbNavigator.SelectedItem.Value == "Business")
                            {
                                index = 4;

                            }
                            
                     
                            else
                            {
                                index = 2;

                            }
                            
                            
                            msg.Status = msg.Status.Remove(index, 1).Insert(index, "2");
                            

                            xpNotifi.Update(msg);
                        }
                        else
                        {
                            xpNotifi.Delete(NotifyID);
                        }

                        //ViewList();
                        rpbNavigator_ItemClick(null, new RadPanelBarEventArgs(rpbNavigator.SelectedItem));
                        InitializeEventRecordsOnMenuPanel();
                    }
                    catch (Exception ex)
                    {
                        return;

                    }
                    break;
                default:

                    break;
            }

        }
        catch (Exception ex)
        {
            e.Canceled = true;
            return;
        }

        //e.Item.OwnerTableView.Rebind();

    }



    private void ViewDetail()
    {
        //rdViewMessage.Visible = true;
        rdList.Visible = false;
        rdNewMessage.Visible = false;
    }
    private void ViewList()
    {
        //rdViewMessage.Visible = false;
        rdList.Visible = true;
        rdNewMessage.Visible = false;
    }
    private void NewMessage()
    {
        //rdViewMessage.Visible = false;
        rdList.Visible = false;
        Updflg.Text = "NewMsg";
        rdNewMessage.Visible = true;
    }

    private void EditMessage(XPNotificationInfo msg)
    {
        //rdViewMessage.Visible = false;
        rdList.Visible = false;
        Updflg.Text = "EditMsg";
        rdNewMessage.Visible = true;

        if (msg != null)
        {
            rcbRecipient.SelectedValue = msg.RecipientUserID.ToString();
            radEditorContent.Content = msg.BodyText;
            tbTitle.Text = msg.Title;
            if (msg.Attachment != string.Empty)
            {
                lnkAttachment.NavigateUrl = msg.Attachment;
                lnkAttachment.Text = Path.GetFileName(msg.Attachment);
            }
        }
    }

    protected void btnCancel_Click(object sender, System.EventArgs e)
    {
        ViewList();
        rcbRecipient.SelectedValue = "";
        rcbRecipient.Text = "";
        tbTitle.Text = "";
        radEditorContent.Content = "";

        lnkAttachment.NavigateUrl = "";
        lnkAttachment.Text = "";
    }

    protected void btnReturn_Click(object sender, System.EventArgs e)
    {
        ViewList();
    }


    protected void btnReply_Click(object sender, System.EventArgs e)
    {

    }

    protected string EnableRead(string qstatus, string Flg)
    {
        string value = "";
        try
        {
            if (rpbNavigator.SelectedItem.Value == "Normal" ||rpbNavigator.SelectedItem.Value == "All"||
                            rpbNavigator.SelectedItem.Value == "Business")
            {
                if (qstatus != null && qstatus.Length > 3
                    && qstatus.Substring(3, 1) == "0")
                {
                    value = "未读";
                }
                else
                {
                    value = "已读";
                }
            }
            else
            {
                value = rpbNavigator.SelectedItem.Text.Substring(0, 10).Trim();
            }
        }
        catch (Exception)
        {
        }
        return value;
    }
    protected string EnableImportant(string qstatus, string Flg)
    {
        string value = "";
        int index = 0;
        try
        {
            //接收者
            if (rpbNavigator.SelectedItem.Value == "Normal" || rpbNavigator.SelectedItem.Value == "All" ||
                rpbNavigator.SelectedItem.Value == "Business")
            {
                index = 4;
            }
            else
            {
                index = 2;
            }
            if (qstatus.Substring(index, 1) == "1")
            {
                if (Flg == "ImageUrl")
                {
                    value = "~/images/backImg/Important_transparent.gif";
                }
                else if (Flg == "AlternateText")
                {
                    value = "点击更改为不重要";
                }
            }
            else
            {
                if (Flg == "ImageUrl")
                {
                    value = "~/images/backImg/Important_grey.gif";
                }
                else if (Flg == "AlternateText")
                {
                    value = "点击更改为重要";
                }
            }

        }
        catch (Exception ex)
        {
            value = "";
        }

        return value;

    }

    protected void rpbNavigator_ItemClick(object sender, Telerik.Web.UI.RadPanelBarEventArgs e)
    {
        if (e.Item.Items.Count > 0)
        {
            //主菜单框

        }
        else
        {
            //条目框

            if (e.Item.Value != null && !string.IsNullOrEmpty(e.Item.Value))
            {
                ViewList();

                //这里将对数据集进行拣选
                switch (e.Item.Value)
                {

                    case "All":
                        sdsMailList.FilterParameters["Filter1"].DefaultValue = " RecipientUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "'AND SUBSTRING([Status], 2, 1) = '1' AND SUBSTRING([Status], 5, 1) <> '2'";
                        break;
                    case "Normal":
                        sdsMailList.FilterParameters["Filter1"].DefaultValue = "  RecipientUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue
                            + "' AND SUBSTRING([Status], 2, 1) = '1'  AND SUBSTRING([Status], 4, 1) = '0'  AND SUBSTRING([Status], 5, 1) <> '2' ";
                        break;
                    case "Business":
                        sdsMailList.FilterParameters["Filter1"].DefaultValue = "  RecipientUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue
                            + "' AND SUBSTRING([Status], 2, 1) = '1'  AND SUBSTRING([Status], 4, 1) = '1' AND SUBSTRING([Status], 5, 1) <> '2'  ";

                        break;
                    case "Saved":
                        //sdsMailList.FilterExpression = "[SendDate] <> '" + DateTime.MinValue.AddYears(1900).ToString("d") + "'";
                        sdsMailList.FilterParameters["Filter1"].DefaultValue = " CreateUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "'  AND SUBSTRING([Status], 1, 1) = '1' AND SUBSTRING([Status], 2, 1) <> '1' AND   SUBSTRING([Status], 3, 1) <> '2'  ";

                        break;
                    case "Sent":
                        //sdsMailList.FilterExpression = "[LastSaveDate] <> '" + DateTime.MinValue.AddYears(1900).ToString("d") + "'";
                        sdsMailList.FilterParameters["Filter1"].DefaultValue = " CreateUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "'  AND SUBSTRING([Status], 2, 1) = '1' AND   SUBSTRING([Status], 3, 1) <> '2'  ";


                        break;
                    case "Important":
                        sdsMailList.FilterParameters["Filter1"].DefaultValue = " ((CreateUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "'  AND SUBSTRING([Status], 3, 1) = '1') " +
                             " OR (RecipientUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "'  AND SUBSTRING([Status], 5, 1) = '1')) ";
                        break;

                    case "Rubbish":
                        sdsMailList.FilterParameters["Filter1"].DefaultValue = " ((CreateUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "'  AND SUBSTRING([Status], 3, 1) = '2')" +
                            " OR (RecipientUserID='" + sdsMailList.SelectParameters["CreateUserID"].DefaultValue + "'  AND SUBSTRING([Status], 5, 1) = '2'))";

                        break;
                    default:

                        return;

                }
                radGridMainlist.Rebind();
            }
        }

    }
    protected void btnSubmitAttachment_Click(object sender, EventArgs e)
    {
        if (radAttachment.UploadedFiles.Count > 0)
        {
            string oldfilename = radAttachment.UploadedFiles[0].GetName();
            string newfilename = null;
            string oldpath = null;
            string newpath = null;
            Trace.Warn(oldfilename);


            oldpath = Server.MapPath(radAttachment.TargetFolder) + "\\" + oldfilename;


            newfilename = FileUtility.CreateUniqueFileName(oldfilename);
            newpath = Server.MapPath(radAttachment.TargetFolder) + "\\" + newfilename;

            System.IO.File.Move(oldpath, newpath);

            lnkAttachment.Text = oldfilename;
            lnkAttachment.NavigateUrl = radAttachment.TargetFolder + "/" + newfilename;

            //将License文件的大小和类型信息存入btn中
            btnSubmitAttachment.CommandName = radAttachment.UploadedFiles[0].ContentType;
            btnSubmitAttachment.CommandArgument = radAttachment.UploadedFiles[0].ContentLength.ToString();


            lnkAttachment.Visible = btnSubmitAttachment.Visible;
            btnDeleteAttachment.Visible = btnSubmitAttachment.Visible;
            btnSubmitAttachment.Visible = !btnDeleteAttachment.Visible;
            radAttachment.Visible = !btnDeleteAttachment.Visible;
        }
    }

    protected void btnDeleteAttachment_Click(object sender, EventArgs e)
    {
        string filepath = Server.MapPath(lnkAttachment.NavigateUrl);

        System.IO.File.Delete(filepath);

        lnkAttachment.Visible = btnSubmitAttachment.Visible;
        btnDeleteAttachment.Visible = btnSubmitAttachment.Visible;
        btnSubmitAttachment.Visible = !btnDeleteAttachment.Visible;
        radAttachment.Visible = !btnDeleteAttachment.Visible;
        sdsCompanyList.DataBind();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        XPNotificationInfo msg = new XPNotificationInfo();
        XPNotification xpNotifi = new XPNotification();

        if (Updflg.Text == "NewMsg")
        {
            msg.CreateUserID = new Guid(sdsMailList.SelectParameters["CreateUserID"].DefaultValue);
            msg.Status = "1000000";
            msg.CreateDate = DateTime.Now;

        }
        else
        {
            Guid NotifyID = new Guid(sdsViewDetail.SelectParameters["NotifyID"].DefaultValue);
            msg = xpNotifi.GetXPNotification(NotifyID);
            msg.Status = msg.Status.Remove(0, 1).Insert(0, "1");
        }
        msg.RecipientUserID = new Guid(rcbRecipient.SelectedValue);
        msg.Attachment = "";
        msg.BodyText = radEditorContent.Content;
        msg.Title = tbTitle.Text;

        if (Updflg.Text == "NewMsg")
        {
            xpNotifi.Insert(msg);
        }
        else
        {
            xpNotifi.Update(msg);
        }

        rcbRecipient.ClearSelection();
        rcbRecipient.Text = "";
        radEditorContent.Content = "";

        InitializeEventRecordsOnMenuPanel();
        //  DataBind();
    }
}